//
//  ColorTableViewCell.m
//  帮惠Mall
//
//  Created by reew on 2017/9/23.
//  Copyright © 2017年 reew. All rights reserved.
//

#import "ColorTableViewCell.h"
#import "ColorCollectionViewCell.h"
#import "UIColor+Hex.m"
#import "System.h"
@interface ColorTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UITextFieldDelegate>
@property (nonatomic,strong) NSMutableArray *selectedColorArr;
@end
@implementation ColorTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.colorCollectionView.scrollEnabled = NO;
    self.colorCollectionView.delegate = self;
    self.colorCollectionView.dataSource = self;
    [self.colorCollectionView registerNib:[UINib nibWithNibName:@"ColorCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ColorCollectionViewCell"];
    
    if (_selectedColorArr == nil) {
        _selectedColorArr = [NSMutableArray array];
    }
    
    
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _mycolorModelArr.count+1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ColorCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ColorCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.row == _mycolorModelArr.count) {
        cell.colorLabel.text = @"自定义颜色";
        cell.colorView.backgroundColor = [UIColor clearColor];
        cell.customColor.delegate = self;
        cell.customColor.hidden = NO;
        cell.colorLabel.hidden = YES;
        cell.colorView.hidden = YES;
    }else{
        MycolorModel *model = _mycolorModelArr[indexPath.row];
        cell.colorView.backgroundColor = [UIColor colorWithHexString:model.colorHex];
        cell.customColor.hidden = YES;
        cell.colorLabel.hidden = NO;
        cell.colorView.hidden = NO;
        cell.colorLabel.text = model.colorName;
    }
    return cell;
}
#pragma mark - UICollectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreenWidth-60)/3, 36);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 10, 5, 10);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ColorCollectionViewCell *cell = (ColorCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (cell.isselected) {
        cell.backgroundColor = [UIColor whiteColor];
        [_selectedColorArr removeObject:_mycolorModelArr[indexPath.row]];
        cell.isselected = NO;
    }else{
        cell.backgroundColor = [UIColor lightGrayColor];
        [_selectedColorArr addObject:_mycolorModelArr[indexPath.row]];
        cell.isselected = YES;
    }
    //代理
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectColorsWithArray:)]) {
        [self.delegate didSelectColorsWithArray:_selectedColorArr];
    }
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    //定义颜色
    if (!(textField.text == nil && textField.text.length ==0 )) {
        MycolorModel *model = [[MycolorModel alloc] initModelWithColorName:textField.text];
        [_mycolorModelArr addObject:model];
        NSIndexPath *index = [NSIndexPath indexPathForRow:_mycolorModelArr.count-1 inSection:0];
        [self.colorCollectionView insertItemsAtIndexPaths:@[index]];
        //代理
        if (self.delegate && [self.delegate respondsToSelector:@selector(didAddColorWithColorName:)]) {
            [self.delegate didAddColorWithColorName:textField.text];
        }
    }
    textField.text = nil;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
